﻿using pdfer.FileObjects;
using pdfer.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pdfer.PdfObjecs
{
    internal class XObjectDictionary : PdfFileObject
    {
        public XObjectDictionary(PdfFileObject pdfFileObject)
            : base(pdfFileObject)
        {
            if (pdfFileObject.TryGetValue(PdfName.Key_Type, out object? val))
            {
                Type = PdfParserUtils.ParseString(val);
                if (Type != PdfTypeConst.Type_XObject)
                {
                    throw new Exception("类型必须是 /XObject");
                }
            }
        }
        /// <summary>
        /// 必须是 XObjecct
        /// </summary>
        public String Type { get; set; }
        private String? _subtype;
        private String? _filter;
        /// <summary>
        /// Image|Form|
        /// </summary>
        public String? SubType
        {
            get
            {
                if (_subtype == null && TryGetValue(PdfName.Key_Subtype, out var obj))
                {
                    _subtype = PdfParserUtils.ParseString(obj);
                }
                return _subtype;
            }
        }
        public String? Filter
        {
            get
            {
                if (_filter == null && TryGetValue(PdfName.Key_Filter, out var obj))
                {
                    _filter = PdfParserUtils.ParseString(obj);
                }
                return _filter;
            }
        }

        #region Image
        private Int32? bitsPerComponent;
        public Int32 BitsPerComponent 
        {
            get
            {
                if (bitsPerComponent == null)
                {
                    if (TryGetValue(PdfName.Key_BitsPerComponent, out var o))
                    {
                        bitsPerComponent = PdfParserUtils.ParseInt32(o, this);
                    }
                }
                return bitsPerComponent ?? 8;
            }
        }
        private Int32? height;
        public Int32 Height 
        { 
            get 
            {
                if(height == null)
                {
                    if(TryGetValue(PdfName.Key_Height, out var o))
                    {
                        height = PdfParserUtils.ParseInt32(o, this);
                    }
                }
                return height ?? 0;
            }
        }
        private Int32? width;

        public Int32 Width
        {
            get
            {
                if (width == null)
                {
                    if (TryGetValue(PdfName.Key_Width, out var o))
                    {
                        width = PdfParserUtils.ParseInt32(o, this);
                    }
                }
                return width ?? 0;
            }
        }
        public Int32 Length { get; set; }
        public String ColorSpace { get; set; }
        #endregion

    }
}
